﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum Result
{
    NOEnd = -1,         // 未结束
    NO = 0,             // 失败
    YES = 1,            // 成功
}

public enum Opened
{
    OPENED_NONE = 0,
    OPENED_NEXT_SCENE = 0x00000001,                     //下一场景打开列表
    OPENED_OPEN_SHOW = 0x00000002,                      //界面打开后检测打开列表
    OPENED_ALL = OPENED_OPEN_SHOW | OPENED_NEXT_SCENE   //全部打开界面完成        
}

/// <summary>
/// 正在打开或者关闭的界面
/// </summary>
public class RunPanel
{
    public int key;
    public bool isDestroy;
    public int closeType = 1;

    public RunPanel(int key, bool isDestroy, int closeType)
    {
        this.key = key;
        this.isDestroy = isDestroy;
        this.closeType = closeType;
    }
}

/// <summary>
/// UI遮罩
/// </summary>
public class LayerInfo
{
    //起始深度
    private int initDepth;
    //当前深度
    public int curDepth;
    //打开界面个数
    private int openNum;

    public LayerInfo(int initDepth)
    {
        this.initDepth = initDepth;
    }

    public void Open()
    {
        openNum++;
    }

    public void Close()
    {
        openNum--;
        if (openNum < 1)
        {
            Reset();
        }
    }

    public void Reset()
    {
        curDepth = initDepth;
    }
}

/// <summary>
/// ui界面管理
/// </summary>
public class UIMgr : IBaseData
{
    /// <summary>
    /// 打开界面过程中
    /// </summary>
    private bool opening;
    /// <summary>
    /// 关闭界面过程中
    /// </summary>
    private bool closeing;
    /// <summary>
    /// 当前正在打开的界面
    /// </summary>
    private int openingId = (int)PanelID.None;
    /// <summary>
    /// 等待关闭正在打开的界面
    /// </summary>
    private int closeingId = (int)PanelID.None;
    /// <summary>
    /// 打开列表
    /// </summary>
    private List<int> OpenList;
    /// <summary>
    /// 关闭列表
    /// </summary>
    private List<int> CloseList;
    /// <summary>
    /// 待打开界面列表 
    /// </summary>
    private List<int> WaitOpenList;
    /// <summary>
    /// 待还原列表
    /// </summary>
    private List<int> WaitRestoreList;
    /// <summary>
    /// 下一个场景要还原列表
    /// </summary>
    private List<int> SceneRestoreList;
    /// <summary>
    /// 绑定场景打开列表
    /// </summary>
    private Dictionary<string, List<int>> SceneOpenList;
    /// <summary>
    /// 注册列表
    /// </summary>
    private Dictionary<int, IMediator> RegistedList;
    /// <summary>
    /// 层次信息列表
    /// </summary>
    private Dictionary<int, LayerInfo> LayerInfoList;
    /// <summary>
    /// 界面层级信息
    /// </summary>
    private List<RunPanel> runPanelList;
    /// <summary>
    /// 打开完成 状态
    /// </summary>
    private Opened opened = Opened.OPENED_NONE;

    public GameObject MainCam { get; private set; }
    public int TopKey { get; private set; }
    public int OpenKey { get; private set; }

    public UIMgr()
    {
        CloseList = new List<int>();
        OpenList = new List<int>();
        WaitRestoreList = new List<int>();
        WaitOpenList = new List<int>();
        SceneRestoreList = new List<int>();

        runPanelList = new List<RunPanel>();
        RegistedList = new Dictionary<int, IMediator>();
        SceneOpenList = new Dictionary<string, List<int>>();
        LayerInfoList = new Dictionary<int, LayerInfo>(); Init();

        ZEventCenter.AddEventListener(ConstData.RETURN_PANEL, OnBackHandler);
        ZEventCenter.AddEventListener(ConstData.SCENE_LOAD_START, OnLoadSceneStart);
        ZEventCenter.AddEventListener(ConstData.SCENE_LOAD_END, OnLoadSceneEnd);
    }
    
    public void Init()
    {
        ClearData(); //清除所有列表
        for (int i = 1; i <= DBSystemMgr.GetSingleton().GetGlobalInt(1); i++)
        {
            int depth = DBSystemMgr.GetSingleton().GetGlobalInt(2) * i;
            LayerInfoList.Add(i, new LayerInfo(depth));
            LayerInfoList[i].Reset();
        }
    }

    public void ClearData()
    {
        OpenList.Clear();
        CloseList.Clear();
        WaitRestoreList.Clear();
        WaitOpenList.Clear();
        SceneRestoreList.Clear();

        runPanelList.Clear();
        RegistedList.Clear();
        SceneOpenList.Clear();
        LayerInfoList.Clear();
    }

    public void SetOpeningStatus(bool bEnd)
    {
        opening = bEnd;
    }

    private bool CheckOpened(Opened opened)
    {
        return (this.opened & opened) == opened;
    }

    private void SetOpened(Opened opened)
    {
        this.opened |= opened;
    }

    public bool IsOpenListHasUI(int key)
    {
        return OpenList.Contains(key);
    }

    private void InitDepth()
    {
        for (int i = 1; i <= LayerInfoList.Count; i++)
        {
            LayerInfoList[i].Reset();
        }
    }
    
    /// <summary>
    /// 设置打开或关闭是否完成
    /// </summary>
    /// <param name="OpenOrClose"></param>
    /// <param name="bFinish"></param>
    public void SetCompleteStatus(bool OpenOrClose, bool bFinish)
    {
        if (OpenOrClose) { opening = bFinish; }
        else { closeing = bFinish; }
    }

    /// <summary>
    /// 添加还是移除场景还原列表
    /// </summary>
    /// <param name="key"></param>
    /// <param name="isAdd"></param>
    public void BindSceneOpen(int key, bool isAdd)
    {
        if (RegistedList.ContainsKey(key))
        {
            if (isAdd) { SceneRestoreList.Add(key); }
            else { SceneRestoreList.Remove(key); }
        }
    }

    /// <summary>
    /// 注册绑定场景的 界面
    /// </summary>
    /// <param name="sceneName"></param>
    /// <param name="mdt"></param>
    public void RegisterSceneOpen(string sceneName, IMediator mdt)
    {
        int key = mdt.PanelId;
        if (!RegistedList.ContainsKey(key))
        {
            RegistedList.Add(key, mdt);
        }
        if (!SceneOpenList.ContainsKey(sceneName))
        {
            SceneOpenList.Add(sceneName, new List<int>());
        }
        List<int> list = SceneOpenList[sceneName];
        if (!list.Contains(key)) { list.Add(key); }
    }

    /// <summary>
    /// 注册一个界面
    /// </summary>
    /// <param name="mdt">界面管理</param>
    public void Register(IMediator mdt)
    {
        int key = mdt.PanelId;
        if (!RegistedList.ContainsKey(key))
        {
            RegistedList.Add(key, mdt);
        }
    }

    /// <summary>
    /// 通过id获取mdt
    /// </summary>
    /// <param name="key"></param>
    public IMediator GetMdtByID(int key)
    {
        IMediator mdt = null;
        RegistedList.TryGetValue(key, out mdt);
        return mdt;
    }

    private void OnBackHandler(ZEvent ev)
    {
        for (int i = OpenList.Count - 1; i >= 0; i--)
        {
            var uiData = GetUIPathDataById(OpenList[i]);
            if (uiData.UIType == (int)UIType.TYPE_BASE && uiData.IsBackClose)
            {
                CloseRun(OpenList[i]); break;
            }
        }
    }

    private void OnLoadSceneStart(ZEvent ev)
    {
        CloseAll();
        Resources.UnloadUnusedAssets();
        WaitRestoreList.Clear(); InitDepth();
    }

    private void OnLoadSceneEnd(ZEvent ev)
    {
        opened = Opened.OPENED_NONE;    //清空所有加载完成标志位
        ZEventCenter.AddEventListener(ConstData.OPENED_ALL_COMPLETE, OnOpenAllComplete);
        if (OpenSenceList())
        {
            ZEventCenter.DispatchEvent(new ZEvent(ConstData.OPENED_ALL_COMPLETE));
        }
        MainCam = GameObject.Find("MainCamera"); Adjust3DCamera();
    }

    private void OnOpenAllComplete(ZEvent ev)
    {
        if (!CheckOpened(Opened.OPENED_NEXT_SCENE))
        {
            SetOpened(Opened.OPENED_NEXT_SCENE);
            if (OpenNextSceneList())
            {
                ZEventCenter.DispatchEvent(new ZEvent(ConstData.OPENED_ALL_COMPLETE));
            }
        }
        else if (!CheckOpened(Opened.OPENED_OPEN_SHOW))
        {
            SetOpened(Opened.OPENED_OPEN_SHOW);
        }

        if (CheckOpened(Opened.OPENED_ALL))
        {
            ZEventCenter.RemoveEventListener(ConstData.OPENED_ALL_COMPLETE, OnOpenAllComplete);
        }
    }

    /// <summary>
    /// 关闭界面的主要目的，是走一遍关闭流程，保证所有界面的事件移除，DialogBase没必要关
    /// </summary>
    public void ClearResource()
    {
        for (int i = OpenList.Count - 1; i >= 0; i--)
        {
            int key = OpenList[i];
            CloseRun(key, true, 3, true);
        }
    }

    /// <summary>
    /// 关闭所有界面
    /// </summary>
    private void CloseAll()
    {
        int key;
        for (int i = OpenList.Count - 1; i >= 0; i--)
        {
            key = OpenList[i];
            var uiData = GetUIPathDataById(key);
            if (uiData.DestroyUnload) { CloseRun(key, true, 3); }
        }

        for (int i = CloseList.Count - 1; i >= 0; i--)
        {
            key = CloseList[i];
            var uiData = GetUIPathDataById(key);
            if (!uiData.DestroyUnload) { continue; }

            IMediator mdt = RegistedList[key];
            if (mdt.Go)
            {
                Object.Destroy(mdt.Go);
                mdt.Destroy();
                CloseList.Remove(key);
            }
        }
    }

    /// <summary>
    /// 打开下个场景ui绑定列表
    /// </summary>
    private bool OpenSenceList()
    {
        int openNum = 0;
        string sceneName = ZMgr.SceneMgr.curSceneName;
        if (SceneOpenList.ContainsKey(sceneName))
        {
            List<int> sceneList = SceneOpenList[sceneName];
            for (int i = 0; i < sceneList.Count; i++)
            {
                Open(sceneList[i]);
                openNum++;
            }
        }
        return openNum == 0;
    }

    /// <summary>
    /// 打开下个场景ui还原列表
    /// </summary>
    public bool OpenNextSceneList()
    {
        int openNum = 0;
        for (int i = 0; i < SceneRestoreList.Count; i++)
        {
            Open(SceneRestoreList[i]);
            openNum++;
        }
        SceneRestoreList.Clear();
        return openNum == 0;
    }

    /// <summary>
    /// 功能开关检测
    /// </summary>
    /// <param name="funcId"></param>
    private bool CheckSysConfig(int funcId)
    {
        if (funcId <= 0) { return true; }   //不需要关联功能开关
        var table_GlobalData = TableDataMgr.GlobalDataMgr.GetItemById(funcId);
        if (table_GlobalData == null)
        {
            return false;
        }

        bool funOpen = SysCfgMgr.GetSingleton().CheckSysConfigSwitch(table_GlobalData.StringParam);
        return funOpen;
    }

    /// <summary>
    /// 打开界面
    /// </summary>
    /// <param name="uidata">ui数据</param>
    public void Open(int key)
    {
        var uiData = GetUIPathDataById(key);
        if (uiData == null) { return; }

        if (uiData.OpenGuideId != -1)
        {
            if (!ZMgr.GuideMgr.IsOpenFun(uiData.OpenGuideId))
            {
                return; //功能未开启
            }
        }
        if (CheckSysConfig(uiData.FuncSwitchId))
        {
            BehaviourUtil.StartCoroutine(OpenRun(key));
        }
    }

    private IEnumerator OpenRun(int key)
    {
        if (!WaitOpenList.Contains(key))
        {
            WaitOpenList.Add(key);
        }

        Label_waitOpen:
        if (opening)
        {
            yield return 0; //等待上一个打开再往下处理
            goto Label_waitOpen;
        }
        if (WaitOpenList.Count < 1)
        {
            yield break;
        }

        openingId = WaitOpenList[0];
        WaitOpenList.RemoveAt(0);
        Result result = Result.NOEnd;

        yield return BehaviourUtil.StartCoroutine(OpenRun(openingId, result));
        if (result == Result.YES)
        {
            AddToOpenList(openingId);
        }
        openingId = (int)PanelID.None;
    }

    private IEnumerator OpenRun(int key, Result result)
    {
        if (RegistedList.ContainsKey(key) && !OpenList.Contains(key))
        {
            var uiData = GetUIPathDataById(key);
            IMediator mdt = RegistedList[key];
            OpenKey = key; opening = true;
            
            if (mdt.Go == null)
            {
                yield return BehaviourUtil.StartCoroutine(Creat(uiData, result));
            }

            CloseList.Remove(key);
            var layerInfo = LayerInfoList[uiData.Layer];
            mdt.Panel.depth = layerInfo.curDepth;
            layerInfo.curDepth += uiData.PanelNum;
            layerInfo.Open();

            if (uiData.IsShowBg == 1)
            {
                ZMgr.SceneBgMgr.ChangeBG(uiData.SceneBgName);
            }
            else if (uiData.IsShowBg == -1)
            {
                ZMgr.SceneBgMgr.CloseSceneBg();
            }

            if (uiData.LockType != 0 || uiData.UIType == (int)UIType.TYPE_SON)
            {
                if (uiData.LockType == 2)
                {
                    ZMgr.LockUIMgr.AddLock(key);
                }
            }

            if (uiData.Show3D > -1 && MainCam)
            {
                Camera mainCamera = MainCam.GetComponent<Camera>();
                mainCamera.enabled = uiData.Show3D == 1;
            }

            SetBlur(mdt);
            Utility.SetActive(mdt.Go, true);
            Utility.SetActive(mdt.Mask, true);

            //不需要加载的打开，直接加入打开列表
            mdt.Open(); AddToOpenList(key);

            Label_waitAnimation:
            if (closeing)
            {
                mdt.SetLocalScale(Vector3.zero);
                //等待上一个关闭再往下处理（其实是等待动画播放完成的时间）
                yield return 0;
                goto Label_waitAnimation;
            }

            mdt.SetLocalScale(Vector3.one);
            //派发一个打开新界面消息
            if (mdt.Tween && uiData.IsPlayTween)
            {
                EventDelegate.Add(mdt.Tween.onFinished, OpenTweenOkFun, true);
                mdt.Tween.PlayForward();
                yield return new WaitForSeconds(mdt.Tween.duration + mdt.Tween.delay);
            }
            else
            {
                OpenTweenOkFun();
            }
            
            //界面关联的镜头动画
            if (uiData.CameraMovieId > 0)
            {
                ZMgr.CameraMovieMgr.PlayMovie(uiData.CameraMovieId);
            }
            if (key != (int)PanelID.GuidePanel)
            {
                TopKey = key;
            }
            opening = false;
        }

        result = Result.NO;
        if (WaitOpenList.Count < 1)
        {
            ZEventCenter.DispatchEvent(new ZEvent(ConstData.OPENED_ALL_COMPLETE));
        }
        yield break;
    }
    
    public IEnumerator Creat(Table_UIPathItem uiData, Result result)
    {
        ZMgr.LoadWaitMgr.OpenLoadingWait(WaitType.WaitUI, uiData.Id.ToString());
        string path = PathUtil.PanelUrl + uiData.Path + ".prefab.uab";
        IMediator mdt = RegistedList[uiData.Id];

        OnLoadArg arg = new OnLoadArg();
        yield return BehaviourUtil.StartCoroutine(ZMgr.ResourceMgr.CoroutineLoad(path, arg));
        GameObject go = arg.asset as GameObject;
        if (go == null)
        {
            result = Result.NO; yield break;
        }

        mdt.Go = NGUITools.AddChild(AllUpdate.UIRoot, go);
        //锁屏遮罩
        if (uiData.LockType != 0 || uiData.UIType == (int)UIType.TYPE_SON)
        {
            if (uiData.LockType == 1)
            {
                mdt.Mask = AddFullMask(mdt.Go);
            }
            else
            {
                mdt.Mask = ZMgr.LockUIMgr.MaskGo;
            }
        }
        mdt.Awake();
        ZMgr.LoadWaitMgr.CloseLoadingWait(WaitType.WaitUI, uiData.Id.ToString());
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <param name="key"></param>
    public void Close(int key)
    {
        var uiData = GetUIPathDataById(key);
        if (uiData == null) { return; }

        if (OpenList.Contains(key))
        {
            CloseRun(key);
            BindSceneOpen(key, false);
            
            LayerInfo layerInfo = LayerInfoList[uiData.Layer];
            layerInfo.curDepth--; layerInfo.Close();

            if (uiData.ReId != -1 && !ZMgr.SceneMgr.isloading)
            {
                //关闭成功 还原窗口
                BehaviourUtil.StartCoroutine(RemoveReList(key));
            }
        }
        else
        {
            if (WaitOpenList.Contains(key))
            {
                WaitOpenList.Remove(key);
            }
            else if (openingId == key)
            {
                closeingId = key;
            }
        }
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <param name="key"></param>
    /// <param name="isDestroy">是否强制销毁</param>
    /// <param name="bIsReconnect">是否是重连时为了清除事件走的关闭流程</param>
    private void CloseRun(int key, bool isDestroy = false, int closeType = 1, bool bIsReconnect = false)
    {
        if (OpenList.Contains(key))
        {
            ZMgr.SceneBgMgr.ResetBgLayer();
            var uiData = GetUIPathDataById(key);
            IMediator mdt = RegistedList[key];
            
            if (uiData.LockType != 0 || uiData.UIType == (int)UIType.TYPE_SON)
            {
                if (uiData.LockType == 2)
                {
                    ZMgr.LockUIMgr.DelLock(key);
                }
            }

            DelBlur(mdt);
            if (mdt.Tween && uiData.IsPlayTween)
            {
                closeing = true;
                RunPanel runPanel = new RunPanel(key, isDestroy, closeType);
                runPanelList.Add(runPanel);

                Utility.SetActive(mdt.Mask, false);
                EventDelegate.Add(mdt.Tween.onFinished, CloseTweenOkFun, true);
                mdt.Tween.PlayReverse();
            }
            else
            {
                closeing = false;   //没有动画的不需要等待
                CloseOk(key, isDestroy, closeType, bIsReconnect);
            }
        }
    }

    /// <summary>
    /// 成功关闭界面处理
    /// </summary>
    /// <param name="key"></param>
    /// <param name="isDestroy"></param>
    /// <param name="closeType"></param>
    /// <param name="bIsReconnect"></param>
    private void CloseOk(int key, bool isDestroy = false, int closeType = 1, bool bIsReconnect = false)
    {
        var uiData = GetUIPathDataById(key);
        IMediator mdt = RegistedList[key];
        //通知管理已close 管理层做相应逻辑处理
        mdt.Close(); closeing = false;
        if (uiData.Destroy || isDestroy)
        {
            mdt.Destroy();
        }
        else
        {
            //关闭,如果是重连的时候，不把预设隐藏，只是走一遍流程
            if (!bIsReconnect)
            {
                Utility.SetActive(mdt.Go, false);
            }
            CloseList.Add(key);
        }

        //从开启列表删除
        OpenList.Remove(key);
        if (OpenList.Count > 0)
        {
            int id = OpenList[OpenList.Count - 1];
            var curUiData = GetUIPathDataById(id);
            //如果打开列表不为空 更换背景为上次打开的UI背景
            ZMgr.SceneBgMgr.ChangeBG(curUiData.SceneBgName, true);
        }
        ZEventCenter.DispatchEvent(new ZEvent(ConstData.CLOSE_PANEL, key));
    }

    /// <summary>
    /// 添加到打开列表
    /// </summary>
    /// <param name="key"></param>
    public void AddToOpenList(int key)
    {
        var uiData = GetUIPathDataById(key);
        if (uiData.ReId != -1 && uiData.UIType != (int)UIType.TYPE_MENU)
        {
            AddRestoreList();
        }
        OpenList.Add(key);

        if (key == closeingId)
        {
            closeingId = (int)PanelID.None;
            CloseRun(key);
        }
    }

    /// <summary>
    /// 还原窗口
    /// </summary>
    private void AddRestoreList()
    {
        for (int i = OpenList.Count - 1; i >= 0; i--)
        {
            int key = OpenList[i];
            var uiData = GetUIPathDataById(key);
            if (uiData.UIType == (int)UIType.TYPE_MENU || uiData.UIType == (int)UIType.TYPE_ERROR
                || uiData.UIType == (int)UIType.TYPE_GUIDE || !RegistedList[key].IsOpen)
            {
                continue;
            }
            if (OpenList.Contains(key))
            {
                CloseRun(key, false, 2);
                if (uiData.UIType != (int)UIType.TYPE_SON)
                {
                    //关闭成功 加入还原列表
                    WaitRestoreList.Add(key);
                }
            }
        }
    }
    
    private IEnumerator RemoveReList(int key)
    {
        if (WaitRestoreList.Count < 1) { yield break; }
        var uiData = GetUIPathDataById(key);

        Label_waitClose:
        if (RegistedList[key].IsOpen)
        {
            yield return 0;
            goto Label_waitClose;
        }
        int reId; int count = WaitRestoreList.Count;

        //还原列表是否有指定的 没有走默认返回
        if (WaitRestoreList.Contains(uiData.ReId))
        {
            reId = uiData.ReId;
        }
        else
        {
            reId = WaitRestoreList[count - 1];
        }

        int index = WaitRestoreList.IndexOf(reId);
        WaitRestoreList.RemoveRange(index, count - index);

        Result result = Result.NOEnd;
        yield return BehaviourUtil.StartCoroutine(OpenRun(reId, result));

        if (result == Result.YES)
        {
            OpenList.Add(reId);
            if (WaitOpenList.Count > 0)
            {
                key = WaitOpenList[0];
                WaitOpenList.Remove(reId); Open(reId);
            }
        }
    }

    private void OpenTweenOkFun()
    {
        ZEventCenter.DispatchEvent(new ZEvent(ConstData.OPENED_PANEL, OpenKey));
    }

    private void CloseTweenOkFun()
    {
        if (runPanelList.Count > 0)
        {
            int idx = runPanelList.Count - 1;
            var runPanel = runPanelList[idx];
            runPanelList.RemoveAt(idx);
            CloseOk(runPanel.key, runPanel.isDestroy, runPanel.closeType);
        }
    }

    /// <summary>
    /// 设置主相机视野
    /// </summary>
    private void Adjust3DCamera()
    {
        if (MainCam)
        {
            UIRoot root = AllUpdate.UIRoot.GetComponent<UIRoot>();
            Camera mainCamera = MainCam.GetComponent<Camera>();
            float height = GlobalNum.HIGH_HEIGHT;
            mainCamera.fieldOfView *= root.manualHeight / height;
        }
    }

    /// <summary>
    /// 添加全屏遮罩
    /// </summary>
    /// <param name="go"></param>
    public GameObject AddFullMask(GameObject go)
    {
        string path = PathUtil.UITextureUrl + "FullMask";
        GameObject fullGO = ZMgr.ResourceMgr.LoadFixedRes(path) as GameObject;
        return NGUITools.AddChild(go, fullGO);
    }

    public void SetBlur(IMediator mdt)
    {
        var uiData = GetUIPathDataById(mdt.PanelId);
        if (uiData.IsFuzz == 1)
        {
            UIBlurMgr.GetSingleton().AddBlur();
            UIBlurMgr.GetSingleton().AddUnBlur(mdt.Go);
            List<GameObject> list = GetBlurTop(mdt);

            for (int i = 0; i < list.Count; i++)
            {
                UIBlurMgr.GetSingleton().AddUnBlur(list[i]);
            }
            list.Clear();
        }
        else if (uiData.IsFuzz == -1)
        {
            UIBlurMgr.GetSingleton().AddUnBlur(mdt.Go);
        }
        else if (uiData.IsFuzz == 0)
        {
            if (UIBlurMgr.GetSingleton().IsOpen && (int)uiData.ReId == -1)
            {
                UIBlurMgr.GetSingleton().AddUnBlur(mdt.Go);
            }
        }
    }

    public void DelBlur(IMediator mdt)
    {
        var uiData = GetUIPathDataById(mdt.PanelId);
        if (uiData.IsFuzz == 1)
        {
            UIBlurMgr.GetSingleton().RemoveBlur();
            List<IMediator> list = new List<IMediator>();
            for (int i = 0; i < OpenList.Count; i++)
            {
                if (OpenList[i] != mdt.PanelId)
                {
                    list.Add(RegistedList[OpenList[i]]);
                }
            }

            list.Sort((IMediator a, IMediator b) =>
            {
                if (a.Panel.depth > b.Panel.depth)
                {
                    return 1;
                }
                return -1;
            });

            for (int i = 0; i < list.Count; i++)
            {
                var temp = GetUIPathDataById(list[i].PanelId);
                if (temp.IsFuzz == 1) { break; }
                if (list[i].Panel.depth > mdt.Panel.depth)
                {
                    UIBlurMgr.GetSingleton().AddUnBlur(list[i].Go);
                }
            }
        }
    }

    public List<GameObject> GetBlurTop(IMediator mdt)
    {
        List<GameObject> list = new List<GameObject>();
        for (int i = 0; i < OpenList.Count; i++)
        {
            IMediator mediator = RegistedList[OpenList[i]];
            if (mediator.Panel.depth > mdt.Panel.depth)
            {
                list.Add(mediator.Go);
            }
        }
        return list;
    }

    public Table_UIPathItem GetUIPathDataById(int id)
    {
        return TableDataMgr.UIPathMgr.GetItemById(id);
    }
}